﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Manager.gui
{
	public partial class PanelAttributes : UserControl
	{
		private MadiManager manager;
		public PanelAttributes()
		{
			InitializeComponent();
		}
		public void updateFields()
		{
			manager = MadiManager.get();
			listViewAttributes.ItemsSource = manager.data.Attributes;
		}
		private void bNuevoAtt_Click(object sender, RoutedEventArgs e)
		{
			gui.DialogNewAttribute dlg = new gui.DialogNewAttribute();

			dlg.ShowDialog();
			if (dlg.DialogResult == true)
			{
				Attribute a = new Attribute();
				a.Name = dlg.NameString;
				a.ShortName = dlg.ShortName;
				a.Value = dlg.Value;
				manager.data.Attributes.Add(a);
				manager.isModified = true;
			}
		}

		private void bEditarAtt_Click(object sender, RoutedEventArgs e)
		{
			Attribute q = (Attribute)listViewAttributes.SelectedItem;
			if (q != null)
			{
				gui.DialogNewAttribute dlg = new gui.DialogNewAttribute(q);

				dlg.ShowDialog();
				if (dlg.DialogResult == true)
				{
					Attribute a = new Attribute();
					a.Name = dlg.NameString;
					a.ShortName = dlg.ShortName;
					a.Value = dlg.Value;
					int posicion = manager.data.Attributes.IndexOf(q);
					manager.data.Attributes.Insert(posicion, a);
					manager.data.Attributes.Remove(q);
					manager.isModified = true;

				}
			}
		}

		private void listViewAttributes_MouseDoubleClick(object sender, MouseButtonEventArgs e)
		{
			bEditarAtt_Click(sender, e);
		}

		private void bEliminarAtt_Click(object sender, RoutedEventArgs e)
		{
			Attribute r = (Attribute)listViewAttributes.SelectedItem;
			if (r != null)
			{
				MessageBoxResult result =
					  MessageBox.Show(
						"¿Estás seguro de que deseas eliminar este atributo? Esta acción no se puede deshacer.",
						"¿Elimiar atributo '" + r.Name + "'?",
						MessageBoxButton.YesNo,
						MessageBoxImage.Warning);
				if (result == MessageBoxResult.Yes)
				{
					manager.data.Attributes.Remove(r);
					manager.isModified = true;
				}
			}
		}
	}
}
